package com.itbbs.db.dataSource;

import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

/**
 * 对service的方法进行增强
 *
 * @author y
 * @date 2021-06-01-18:14
 */
@Aspect
@Component
public class DataSourceAop {

    @Pointcut("execution(* com.itbbs.*.mapper.*.select*(..))" +
            "|| execution(* com.itbbs.*.mapper.*.get*(..))")
    private void readPointcut(){};

    @Pointcut("execution(* com.itbbs.*.mapper.*.insert*(..))" +
            "|| execution(* com.itbbs.*.mapper.*.save*(..))" +
            "|| execution(* com.itbbs.*.mapper.*.update*(..))" +
            "|| execution(* com.itbbs.*.mapper.*.edit*(..))" +
            "|| execution(* com.itbbs.*.mapper.*.delete*(..))" +
            "|| execution(* com.itbbs.*.mapper.*.remove*(..))")
    private void writePointcut(){};

    @Pointcut("readPointcut()"+"|| writePointcut()")
    private void removePointcut(){};

    @Before("readPointcut()")
    public void read(){
        DBContextHolder.slave();
    }

    @Before("writePointcut()")
    public void write(){
        DBContextHolder.master();
    }

    @After("removePointcut()")
    public void remove(){
        DBContextHolder.remove();
    }

    @AfterThrowing("removePointcut()")
    public void err(){
        DBContextHolder.remove();
    }
}
